package kr.saeildamil.androidstudy_second20;

import kr.saeildamil.androidstudy_second.R;
import kr.saeildamil.androidstudy_second20.common.CounterFragment;
import kr.saeildamil.androidstudy_second20.common.TextFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmentmanagertest);
	}
	
	public void mOnClick(View v){
		FragmentManager fm=getFragmentManager();
		Fragment fragment=fm.findFragmentById(R.id.ch20_fragmentmanagertest_fragment);
		
		switch (v.getId()) {
		case R.id.ch20_fragmentmanagertest_add:
			if(fragment==null){
				CounterFragment cf=new CounterFragment();
				/*FragmentTransaction ft=fm.beginTransaction();
				ft.add(R.id.ch20_fragmentmanagertest_fragment, cf, "counter");
				ft.commit();*/
				fm.beginTransaction().add(R.id.ch20_fragmentargument_fragment,cf, "counter").commit();
			}else{
				Toast.makeText(this, "이미 프래그먼트가 존재합니다", Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.ch20_fragmentmanagertest_remove:
			if(fragment==null){
				Toast.makeText(this, "삭제할 프래그먼트가 없음", Toast.LENGTH_SHORT).show();
			}else{
				fm.beginTransaction().remove(fragment).commit();
			}
			break;
		case R.id.ch20_fragmentmanagertest_replace:
			if(fragment==null){
				Toast.makeText(this, "프래그먼트가 없습니다.", Toast.LENGTH_SHORT).show();
			}else{
				FragmentTransaction ft=fm.beginTransaction();
				if(fragment.getTag()=="counter"){
					TextFragment tf=new TextFragment();
					fm.beginTransaction().replace(R.id.ch20_fragmentmanagertest_fragment,tf, "text");
				}else{
					CounterFragment cf=new CounterFragment();
					fm.beginTransaction().replace(R.id.ch20_fragmentmanagertest_fragment,cf, "counter");
				}
				ft.commit();
			}
			break;
		case R.id.ch20_fragmentmanagertest_hide:
			if(fragment==null){
				Toast.makeText(this, "프래그먼트가 없습니다.", Toast.LENGTH_SHORT).show();
			}else{
				FragmentTransaction ft=fm.beginTransaction();
				if(fragment.isHidden()){
					ft.show(fragment);
				}else{
					ft.hide(fragment);
				}
				ft.commit();
			}
			break;
		}
	}
}
